  package com.recommender;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import Neighborhood.Neighborhood;

import com.model.DataModel;
import com.model.StructuredContextDataModel;
import com.similarity.ContextSimilarity;

import edu.emory.mathcs.backport.java.util.TreeMap;


public class SimpleRecommender extends StructuredContextRecommender{

	ContextSimilarity cSim;
	Neighborhood neighborhood;
	


	@Override
	public Map<Integer, Double> getRecommendations(
			HashMap<String, String> contextMap, String keyword, long userID,
			DataModel datamodel,Neighborhood neighborhood) {
		
		return getRecommendations(contextMap, keyword, datamodel,neighborhood);
	}
	
	

	public Map<Integer,Double> getRecommendations(HashMap<String, String> contextMap,String keyword,DataModel datamodel,Neighborhood neighborhood) {
		
		ResultSet tuples=null;
		genericmodel=(StructuredContextDataModel)datamodel;
		this.neighborhood=neighborhood;
		cSim=new ContextSimilarity(datamodel);
		String[] contextVariables=genericmodel.getContextVariables();

		boolean inserted=false;
		HashMap<Integer, Double> myHashMap = new HashMap<Integer,Double>();
		Map<Integer, Double> imageResultsMap=new TreeMap();
				
				
		try {
			tuples=genericmodel.getContextDetails(keyword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			while(tuples.next())
			{
				double predictedRank=0.0f;
				int imageID=tuples.getInt("ImageID");
				System.out.println(contextVariables);
				for (String s : contextVariables) {
					
					if(s.equals("Keyword")){
						System.out.println(s);
						continue;}
					else if(s.equals("UserID")){
						System.out.println(s);
						continue;}
					else	
					{
						String contextVariable=s;
						String contextValue= tuples.getString(contextVariable);
						String currentContextValue= contextMap.get(contextVariable.toLowerCase());
						double contextSimilarity=cSim.calculateSimilarity(contextVariable,contextValue,currentContextValue);
						System.out.println("Sim - "+contextSimilarity);
						double rankForContext=getRankings(imageID, contextVariable, currentContextValue);
						System.out.println("rankForContext - "+rankForContext);
						predictedRank += contextSimilarity*rankForContext;
						
						
					}
					
					System.out.println("ImageID= "+imageID+" predictedRank= "+predictedRank);				
				}
				
				Double rank=0.0d;
				
				
				if(myHashMap.containsKey(imageID))
				{
					rank=(Double)myHashMap.get(imageID);
					inserted=true;
				}
				else inserted=false;
		
				
				if(!inserted)
				{
					myHashMap.put(imageID, predictedRank);
				}
				else
				{
					Double newrank=predictedRank + rank;
					myHashMap.put(imageID,newrank);
				}
				
				
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		imageResultsMap=neighborhood.getFilteredImages(myHashMap);
			
		return imageResultsMap;
	}

	
	



}
